package com.compus_help.controller.admin;

import com.compus_help.common.Result;
import com.compus_help.service.adminDashboardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminDashboardController {
    private static final Logger log = LoggerFactory.getLogger(adminDashboardController.class);

    @Autowired
    adminDashboardService adminDashboardService;
    /**
     * 获取仪表盘统计数据
     */
    @GetMapping("/dashboard/stats")
    public Result getDashboardStats() {
        try {
            Map<String, Object> statsData = adminDashboardService.getDashboardStats();
            log.info("获取仪表盘统计数据成功 {} {} {}", statsData.get("studentsTotal"), statsData.get("ordersTotal"), statsData.get("categoryOrdersTotal"));
            return Result.success(statsData);
        } catch (Exception e) {
            return Result.error("获取失败: " + e.getMessage());
        }
    }
    /**
     * 获取学生按学院分布数据
     */
    @GetMapping("/dashboard/student-distribution")
    public Result getStudentDistributionByCollege() {
        try {
            List<Map<String, Object>> distributionData = adminDashboardService.getStudentDistributionByCollege();
            log.info("获取学生按学院分布数据成功 {}", distributionData.toString());
            return Result.success(distributionData);
        } catch (Exception e) {
            return Result.error("获取失败: " + e.getMessage());
        }
    }
    /**
     * 获取学院列表
     */
    @GetMapping("/colleges")
    public Result getCollegeList() {
        try {
            List<Map<String, String>> collegeList = adminDashboardService.getCollegeList();
            log.info("获取学院列表成功 {}", collegeList.toString());
            return Result.success(collegeList);
        } catch (Exception e) {
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 导出指定学院的学生名单
     */
    @GetMapping("/export/students")
    public ResponseEntity<byte[]> exportStudentsReport(@RequestParam("colleges") String colleges) {
        String[] collegeArray = colleges.split(",");
        byte[] excelData = adminDashboardService.exportStudentsReport(collegeArray);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "学生名单.xlsx");
        return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
    }

}
